package cn.wyl.system.service.impl;

import cn.wyl.system.domain.Role;
import cn.wyl.system.mapper.RoleMapper;
import cn.wyl.system.service.IRoleService;
import cn.wyl.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 业务实现类：
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    @Transactional
    public void save(Role role) {
        super.save(role);
        //添加当前角色的权限 - 前端一定是选择了权限
        if(role.getPermissions()!=null && role.getPermissions().size()>0){
            roleMapper.addRolePermissions(role);
        }
        //添加当前角色的菜单 - 前端一定是选择了菜单
        if(role.getMenus()!=null && role.getMenus().size()>0){
            roleMapper.addRoleMenus(role);
        }
    }

    @Override
    @Transactional
    public void delete(Long id) {
        //删除当前角色的所有权限
        roleMapper.delPermissionsByRoleId(id);
        //删除当前角色的所有菜单
        roleMapper.delMenusByRoleId(id);
        super.delete(id);
    }

    @Override
    @Transactional
    public void update(Role role) {
        super.update(role);
        //删除当前角色的所有权限
        roleMapper.delPermissionsByRoleId(role.getId());
        //删除当前角色的所有菜单
        roleMapper.delMenusByRoleId(role.getId());
        //添加当前角色的权限 - 前端一定是选择了权限
        if(role.getPermissions()!=null && role.getPermissions().size()>0){
            roleMapper.addRolePermissions(role);
        }
        //添加当前角色的菜单 - 前端一定是选择了菜单
        if(role.getMenus()!=null && role.getMenus().size()>0){
            roleMapper.addRoleMenus(role);
        }
    }

    @Override
    public void patchDelete(Long[] ids) {
        for (Long id : ids) {
            delete(id);
        }
    }
}
